//
//  MSLAlignemntView.m
//  GreetCard
//
//  Created by CPX on 2018/9/13.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MSLAlignemntView.h"
#import "MSLNormalSlider.h"

/**
 贺卡对齐方式。。颜色透明度、阴影效果等调节
 */
@interface MSLAlignemntView()
@property (nonatomic,strong)NSMutableArray *buttonArray;
@end


@implementation MSLAlignemntView


- (NSMutableArray *)buttonArray{
    if (!_buttonArray){
        _buttonArray = [[NSMutableArray alloc]init];
    }
    return _buttonArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

-(void)setupView{
 
    NSArray  * imagenames_normal = @[@"align_left",@"align_center",@"align_right"];
    NSArray  * imagenames_select = @[@"align_left_s",@"align_center_s",@"align_right_s"];

    UIView * last = nil;
    for(NSInteger index = 0;index<3;index++){
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:imagenames_normal[index]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:imagenames_select[index]] forState:UIControlStateSelected];
        [self addSubview:button];
        [self.buttonArray addObject:button];
        button.layer.cornerRadius = 2;
        button.layer.masksToBounds =YES;
        button.layer.borderColor = [UIColor lightGrayColor].CGColor;
        button.layer.borderWidth = 0.5;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (last){
                make.leading.equalTo(last.mas_trailing).offset(10);
                make.top.bottom.equalTo(last);
                make.width.equalTo(last);
                
                if (index == 2){
                    make.trailing.equalTo(self.mas_trailing);
                }
                
            }
            else{
                make.leading.mas_equalTo(0);
                make.bottom.equalTo(self.mas_bottom);
            }
            make.height.mas_equalTo(40);

        }];
        [button addTarget:self action:@selector(aligenAction:) forControlEvents:UIControlEventTouchUpInside];
        
        last = button;

    }
    //首次点击
    [self aligenAction:self.buttonArray.firstObject];
    
    NSString * string_value_opactiy = @"100%";
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]){
        string_value_opactiy = string_value_opactiy.arabicString;
    }
    MSLNormalSlider * sliderContainer_opactiy = [[MSLNormalSlider alloc]initWithTitle:TLString(@"greeting_cards_opactiy") defaultValue:100 value_max:100 valueString:string_value_opactiy];
    
    NSString * string_shadow = @"0";
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]){
        string_shadow = string_shadow.arabicString;
    }
    MSLNormalSlider * sliderContainer_shadow = [[MSLNormalSlider alloc]initWithTitle:TLString(@"greeting_cards_shadow") defaultValue:0 value_max:20 valueString:string_shadow];
    [self addSubview:sliderContainer_shadow];
    [self addSubview:sliderContainer_opactiy];
    [sliderContainer_opactiy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.leading.trailing.top.equalTo(self);

    }];
    [sliderContainer_shadow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.leading.trailing.centerY.equalTo(self);

    }];
    __weak typeof (self) weakSelf = self;

    sliderContainer_opactiy.sliderValueEvent = ^(float value,MSLNormalSlider * sender) {
        NSString *string = [NSString stringWithFormat:@"%d%%",(int)value];
        if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]){
            string = string.arabicString;
        }
        
        
        sender.valueLabel.text = string;
        weakSelf.opactiyEvent?weakSelf.opactiyEvent(value):nil;
    };
    sliderContainer_shadow.sliderValueEvent = ^(float value,MSLNormalSlider * sender) {
        NSString * string = [NSString stringWithFormat:@"%d",(int)value];
        if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]){
            string = string.arabicString;
        }
        
        sender.valueLabel.text = string;

        weakSelf.shadowEvent?weakSelf.shadowEvent(value):nil;
    };
    
    
}

-(void)aligenAction:(UIButton *)sender{
    NSInteger index = [self.buttonArray indexOfObject:sender];
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (sender == obj){
            obj.selected = YES;
            obj.backgroundColor = [UIColor blackColor];
        }
        else
        {
            obj.selected = NO;
            obj.backgroundColor = [UIColor whiteColor];

        }
    }];
    
    if (self.alignEvent){
        switch (index) {
            case 0:
                {
                    self.alignEvent(NSTextAlignmentLeft);
                    break;

                }
            case 1:
            {
                self.alignEvent(NSTextAlignmentCenter);
                break;
                
            }
            case 2:
            {
                self.alignEvent(NSTextAlignmentRight);

                break;
                
            }
                
            default:
                break;
        }
    }

    
}

@end
